﻿// <copyright file="IEBrowserLanguagePreference.cs" company="None">(c) Guy Smith-Ferrier, 2012</copyright>

namespace Browser
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime.InteropServices;
    using Microsoft.Win32;

    /// <summary>
    /// IEBrowserLanguagePreference is a class to get and/or set the Internet Explorer Language Preference
    /// </summary>
    /// <remarks>You have to reopen IE before you can see the effect of any change.</remarks>
    public class IEBrowserLanguagePreference : IBrowserLanguagePreference
    {
        /// <summary>
        /// Gets or sets the Internet Explorer registry key
        /// </summary>
        [SuppressMessage("Microsoft.StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = "This is not Hungarian notation")]
        private RegistryKey ieRegistryKey;

        /// <summary>
        /// IE_SET_OPTIONS is an enumeration for Internet Explorer options for InternetSetOptions
        /// </summary>
        private enum IE_SET_OPTIONS
        {
            /// <summary>
            /// INTERNET_OPTION_REFRESH tells Internet Explorer to refresh its settings
            /// </summary>
            INTERNET_OPTION_REFRESH = 37,

            /// <summary>
            /// INTERNET_OPTION_SETTINGS_CHANGED tells Internet Explorer that the settings have been changed
            /// </summary>
            INTERNET_OPTION_SETTINGS_CHANGED = 39
        }

        /// <summary>
        /// Gets or sets the browser's language preferences
        /// </summary>
        public string LanguagePreferences
        {
            get
            {
                return this.IERegistryKey.GetValue("AcceptLanguage").ToString();
            }

            set
            {
                this.IERegistryKey.SetValue("AcceptLanguage", value);
                RefreshInternetExplorerSettings();
            }
        }

        /// <summary>
        /// Gets a value indicating whether the browser exists on this computer
        /// </summary>
        public bool BrowserExists
        {
            get
            {
                return Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\International", true) != null;
            }
        }

        /// <summary>
        /// Gets the Internet Explorer registry key
        /// </summary>
        protected RegistryKey IERegistryKey
        {
            get
            {
                if (this.ieRegistryKey == null)
                {
                    this.ieRegistryKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\International", true);
                }

                return this.ieRegistryKey;
            }
        }

        [DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern bool InternetSetOption(
            IntPtr handleInternet,
            IE_SET_OPTIONS option,
            IntPtr buffer,
            int bufferLength);

        /// <summary>
        /// RefreshInternetExplorerSettings tells Internet Explorer to refresh its settings
        /// </summary>
        /// <returns>True if the refresh was successful</returns>
        private static bool RefreshInternetExplorerSettings()
        {
            bool success = InternetSetOption(IntPtr.Zero, IE_SET_OPTIONS.INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
            success = InternetSetOption(IntPtr.Zero, IE_SET_OPTIONS.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0) && success;
            return success;
        }
    }
}
